package com.gongzhiyun.springboot3test.mapper.dialogue;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gongzhiyun.springboot3test.entity.dialogue.PrivateMessages;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface PrivateMessagesMapper extends BaseMapper<PrivateMessages> {

    @Select("select sender_id 'uid' from private_messages where receiver_id=#{uid} union select receiver_id from private_messages where sender_id=#{uid}")
    List<Integer> getPrivateUid(Integer uid);

    @Select("select  * from private_messages where (receiver_id=#{uid} and sender_id=${id}) or (receiver_id=#{id} and sender_id=${uid}) order by sent_at")
    List<PrivateMessages> getDialogue(Integer uid,Integer id);

    @Select("select  content from private_messages where (receiver_id=#{uid} and sender_id=${id}) or (receiver_id=#{id} and sender_id=${uid}) order by sent_at desc limit 1")
    String getLastWord(Integer uid,Integer id);

    @Select("select count(*) from private_messages where is_read=false and (receiver_id=#{uid} and sender_id=#{id})")
    Integer getUnreadCount(Integer uid,Integer id);

    @Update("update private_messages set is_read=true where is_read=false and receiver_id=#{uid} and sender_id=#{id};")
    Integer read(Integer uid,Integer id);
}
